package com.example.demo.loop.controller;

import com.example.demo.loop.domain.*;
import com.example.demo.loop.mapper.StudentMapper;
import com.example.demo.loop.service.StudentMassage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.ResponseWrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: daiguoqing
 * @Date: 2020-08-31
 * @Time: 15:49
 * @Version: hello-spring-boot 1.0
 */
@Controller
public class StudentController {

//    @SuppressWarnings("all")   //controller被事务管理
    @Autowired  //注入service层
    public StudentMassage stumas;

    //新增查询班级和学科的信息
    @RequestMapping("/look")
    public String SelectUserlo(Model model){
        List<Grade> grades = stumas.selectGrade();
        List<Elective> electives = stumas.selectElective();
        model.addAttribute("grade",grades);
        model.addAttribute("elective",electives);
        return "add";
    }

    //新增学生信息   post
    //新增完成，跳转到首页
    @RequestMapping("/look/user")
    public String  AddUser(User user){
        stumas.AddUser(user);
        //从定向查询所有
        return "redirect:/index";
    }

    //删除学生信息   delete
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)      //两个参数以上使用@requestparam
    @ResponseBody  // 将数据返回给前端，ajax会判断是否删除成功
    public String deleteUser(Long id){
        stumas.deleteUser(id);
        return "1";//
    }

//    条件查询  姓名 班级
    @RequestMapping("/user/koop")
    public String selectUser(String cname,  //查询条件
                             long gradeId,
                             Model model,
                             @RequestParam(value = "start",defaultValue = "0") Integer start

    ){
        //班级  学科
        List<Grade> grades = stumas.selectGrade();
        List<Elective> electives = stumas.selectElective();
        model.addAttribute("grade",grades);
        model.addAttribute("elective",electives);

//        Page<Object> objects = PageHelper.startPage(pageNum, pageSize);

        //查询到参数了
        List<User> users = stumas.selectUser(cname,gradeId);      //一条数据
        for (User user : users) {
            //保存性别
            if(user.getSex().equals("1")){
                user.setSex("男");
            }else if(user.getSex().equals("2")){
                user.setSex("女");
            }
        }
        PageInfo<User> pageInfo = new PageInfo<User>(users);

        if (pageInfo.getPrePage()==0){
            pageInfo.setPrePage(1);
        }

        if (pageInfo.getNextPage()==0){
            pageInfo.setNextPage(pageInfo.getPages());
        }
        model.addAttribute("pageInfo",pageInfo);
        //如果查询没有同时存在，就从定向到当前/index/pageHel方法
        return "index";
    }
    //条件查询以及分页
    @RequestMapping("/index/pageHel")
    public String findPage(Model model,
                             @RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "5") int pageSize,
                             @RequestParam(required = false) String cname,      //查询条件
                             @RequestParam(required = false) Long gradeId){


        //班级 学科
        List<Grade> grades = stumas.selectGrade();
        List<Elective> electives = stumas.selectElective();
        model.addAttribute("grade",grades);
        model.addAttribute("elective",electives);
        model.addAttribute("searchName",cname);
        model.addAttribute("searchid",gradeId);


        PageHelper.startPage(page,pageSize);
        //将学生信息全部查询
        List<User> users = stumas.UserAll(cname,gradeId);
        for (User user : users) {
            //保存性别
            if(user.getSex().equals("1")){
                user.setSex("男");
            }else if(user.getSex().equals("2")){
                user.setSex("女");
            }
        }
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        if (pageInfo.getPrePage()==0){
            pageInfo.setPrePage(1);
        }

        if (pageInfo.getNextPage()==0){
            pageInfo.setNextPage(pageInfo.getPages());
        }
        model.addAttribute("pageInfo",pageInfo);
        return "index";
    }

    //点击修改按钮将用户信息回显到模态框内
    //将输入模态框的内容修改到数据库中
    //修改用户信息
    @RequestMapping("/updateUser")
    public String updateUser(User user){
        /*User1 user1 = new User1();
        user1.setId(user.getId());
        user1.setName(user.getName());
        user1.setPhone(user.getPhone());
        user1.setAddress(user.getAddress());
        user1.setGName(user.getGName());
        user1.setEName(user.getEName());*/
        if(user.getSex().equals("男")){
            user.setSex("1");
        }else if(user.getSex().equals("女")){
            user.setSex("2");
        }
        stumas.updateUser(user);
        return "redirect:/index/pageHel";
    }

    //通过id查询学生信息
    @RequestMapping("/select/su")
    @ResponseBody
    public User selectUserDto(long id){
        User user = stumas.selectUserDto(id);
        return user;
    }

    //查询全部学生信息
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String selectUserAll(Model model,
                                    @RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "5") int pageSize){


        //班级 学科
        List<Grade> grades = stumas.selectGrade();
        List<Elective> electives = stumas.selectElective();
        model.addAttribute("grade",grades);
        model.addAttribute("elective",electives);


        PageHelper.startPage(page,pageSize);
        List<User> userList = stumas.selectUserAll();
        //将学生信息全部查询
        for (User user : userList) {
            //保存性别
            if(user.getSex().equals("1")){
                user.setSex("男");
            }else if(user.getSex().equals("2")){
                user.setSex("女");
            }
        }
        PageInfo<User> pageInfo = new PageInfo<User>(userList);

        if (pageInfo.getPrePage()==0){
            pageInfo.setPrePage(1);
        }

        if (pageInfo.getNextPage()==0){
            pageInfo.setNextPage(pageInfo.getPages());
        }
        model.addAttribute("pageInfo",pageInfo);
        return "index";
    }

}

/*

//    //查询学生信息
//    @RequestMapping(value = "/index",method = RequestMethod.GET)
////    @RequestMapping(value = {"/","/index.html"})
//    public String UserAll(Model model){
//        List<User> users = stumas.UserAll();
//        //users重名没问题
//        model.addAttribute("userList",users);
//        //班级
//        List<Grade> grades = stumas.selectGrade();
//        List<Elective> electives = stumas.selectElective();
//        model.addAttribute("grade",grades);
//        model.addAttribute("elective",electives);
//        return "index";
//    }

*/
/*//*
/*
/跳转页面index
    @RequestMapping("/stuAdd")
    public String stuAdd(){
        return "add";
    }*//*
*/
/*




*/
